package com.ruoyi.client.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.pojo.form.UserLoginForm;
import com.ruoyi.client.pojo.form.UserRegisterForm;
import com.ruoyi.client.pojo.form.UserUpdateForm;
import com.ruoyi.client.pojo.form.UserUpdatePwdForm;
import com.ruoyi.client.pojo.vo.UserLoginVo;
import com.ruoyi.client.pojo.vo.UserRegisterVo;
import com.ruoyi.client.pojo.vo.UserVo;
import com.ruoyi.client.service.IUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户Controller
 *
 * @author HuaQP
 * @date 2024-09-14
 */
@Anonymous
@Api(tags = "02.用户接口")
@RestController("ClientUserController")
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Resource
    private IUserService userService;

    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R<UserLoginVo> login(@RequestBody  UserLoginForm form)
    {
        //用户登录验证
        UserLoginVo userLoginVo = userService.login(form);

        // 成功返回
        return R.ok(userLoginVo);
    }


    /**
     * 用户注册
     */
    @ApiOperation(value = "用户注册")
    @ApiImplicitParam(name = "form", value = "用户注册参数", dataType = "UserRegisterForm", required = true)
    @PostMapping("/register")
    public R<UserRegisterVo> register(@RequestBody @Valid UserRegisterForm form){
        // 调用服务层
        UserRegisterVo userRegisterVo = userService.register(form);
        // 返回
        return R.ok(userRegisterVo);
    }


    /**
     * 查询我的详情
     */
    @ApiOperation(value = "查询我的详情", notes = "查询当前登录用户的详情信息")
    @GetMapping("/detail")
    public R<UserVo> detail() {
        // 调用服务层
        UserVo userVo = userService.getMyDetail();
        // 返回
        return R.ok(userVo);
    }

    /**
     * 用户登出
     */
    @ApiOperation(value = "用户登出", notes = "清除token")
    @PostMapping("/logout")
    public R<String> logout() {
        StpUtil.logout();
        return R.ok(); //登出成功
    }

    /**
     * 更改用户信息
     */
    @ApiOperation(value = "更改用户信息", notes = "用户id不能为空")
    @ApiImplicitParam(name = "form", value = "用户更改参数", dataType = "UserUpdateForm", required = true)
    @PostMapping("/update")
    public R<String> update(@RequestBody @Valid UserUpdateForm form) {
        return userService.update(form) ? R.ok() : R.fail();
    }

    /**
     * 更改用户密码
     */
    @ApiOperation(value = "更改用户密码", notes = "用户id，新旧密码不能为空")
    @ApiImplicitParam(name = "form", value = "用户更改密码参数", dataType = "UserUpdatePwdForm", required = true)
    @SaCheckLogin
    @PostMapping("/updatePwd")
    public R<String> updatePwd(@RequestBody @Valid UserUpdatePwdForm form) {
        return userService.updatePwd(form) ? R.ok() : R.fail();
    }


}
